# coding=iso-8859-1

import tempfile, subprocess, os
import Tms, wx
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
import reportlab.platypus as platy
from reportlab.lib.units import inch
from reportlab.lib import colors

from reportlab.pdfgen import canvas


ColorTable = {'RegPio':colors.palegreen, 'AuxPio':colors.mediumturquoise,
    'OtherMonth':colors.yellow, 'Day':colors.honeydew, 'Grid':colors.black,
    'TopRow':colors.lightgrey, 'MonthColumn':colors.lightgrey, 'BottomRow':colors.greenyellow}

SmParts = ['', 'SM Part1 ID', 'SM Part2 ID', 'SM Part3 ID', 'SM Part4 ID', 'SM Part5 ID',]
SmSource = ['', 'SM 1 Source', 'SM 2 Source', 'SM 3 Source', 'SM 4 Source', 'SM 5 Source']
AwaySpeakers = ['', 'Away1 Speaker', 'Away2 Speaker', 'Away3 Speaker']
AwayCong = ['', 'Away1 Cong', 'Away2 Cong', 'Away3 Cong']
AwayTheme = ['', 'Away1 Theme', 'Away2 Theme', 'Away3 Theme']

Widths= {'Name':2.4*inch, 'Assistant':2.2*inch, 'Source':1.8*inch, 'Theme':4.5*inch,
    'wRemarks':7.5*inch, 'wSource':7.5*inch, 'smPart':7*inch, 'pTalk':7*inch}
myFont = 'Times-Roman'
myBoldFont = 'Times-Bold'
myFontSize = 12

meetingDays = {'Regular':{'Days':['Thursday', 'Sunday'], 'Thursday':['CBS', 'TMS', 'SM'], 'Sunday':['PT', 'WT']},
    'CO Visit':{'Days':['Tuesday','Thursday','Sunday'], 'Tuesday':['TMS','SM','CO Talk 1'], 'Thursday':['CBS', 'CO Talk 2'], 'Sunday':['PT','WT','CO Talk 3']}}

def drawLineEffects(parts):
    ''' Takes a line of information and draws each part according to a specified effect.
        parts is a dictionary that is defined as follows:
            "canvas" = current canvas we are drawing on
            "startX" = X axis starting position
            "startY" = Y axis starting position
            "lines" = a list where each item is a list that includes:
                1) a string
                2) Font Type
                3) Font Size
                4) True or False based upon whether this line will be underlined
    '''
    lineX = parts['startX']
    lineY = parts['startY']
    c = parts['canvas']
    for line in parts['lines']:
        lineWidth = c.stringWidth(line[0],line[1],line[2])
        c.setFont(line[1],line[2])
        c.drawString(lineX, lineY, line[0])
        if line[3] == True:
            c.line(lineX, lineY+.03*inch, lineX+lineWidth, lineY+.03*inch)
        lineX += lineWidth

def isSecondSchool(self, date):
    school2 = True
    if date in self.p.db.Calendar:
        num = self.p.db.Calendar[date].get('#Schools', self.p.db.Options['#Schools'])
    if num < 2:
        school2 = False
    return school2

def getStudentsSex(self, date, talk):
    id = self.p.db.Calendar[date].get(talk, -1)
    if self.p.db.Students[id].get('Sex', 'Male') == 'Female':
        female = True
    else:
        female = False
    return female

def setFontSize(c, type, info):
    curFont = myFont
    curSize = myFontSize
    maxWidth = Widths[type]
    curWidth = c.stringWidth(info[type], curFont, curSize)
    while curWidth > maxWidth:
        curSize -= .1
        curWidth = c.stringWidth(info[type], curFont, curSize)
    c.setFont(curFont, curSize)

def getAssignmentSlipInfo(self, date, talk):
    #todo add small field at bottom to print bookstudy group that individual belongs to for easy sorting
    talks = {'Highlights':{'Speaker':'H ID', 'Talk':'H', 'Source':'H Source', 'Hall1':'X', 'Hall2':'', 'Hall3':''},
        'Talk 1a':{'Speaker':'1a ID', 'Talk': '1', 'Source':'1 Source', 'Hall1':'X', 'Hall2':'', 'Hall3':'', 'Study':'1a StudyID'},
        'Talk 2a':{'Speaker':'2a ID', 'Talk': '2', 'Assistant':'2a Assist ID', 'Source':'2 Source', 'Theme':'2 Theme', 'Hall1':'X', 'Hall2':'', 'Hall3':'', 'Study':'2a StudyID'},
        'Talk 3a':{'Speaker':'3a ID', 'Talk': '3', 'Assistant':'3a Assist ID', 'Source':'3 Source', 'Theme':'3 Theme', 'Hall1':'X', 'Hall2':'', 'Hall3':'', 'Study':'3a StudyID'},
        'Talk 1b':{'Speaker':'1b ID', 'Talk': '1', 'Source':'1 Source', 'Hall1':'', 'Hall2':'X', 'Hall3':'', 'Study':'1b StudyID'},
        'Talk 2b':{'Speaker':'2b ID', 'Talk': '2', 'Assistant':'2b Assist ID', 'Source':'2 Source', 'Theme':'2 Theme', 'Hall1':'', 'Hall2':'X', 'Hall3':'', 'Study':'2b StudyID'},
        'Talk 3b':{'Speaker':'3b ID', 'Talk': '3', 'Assistant':'3b Assist ID', 'Source':'3 Source', 'Theme':'3 Theme', 'Hall1':'', 'Hall2':'X', 'Hall3':'', 'Study':'3b StudyID'},
        'Talk 1c':{'Speaker':'1c ID', 'Talk': '1', 'Source':'1 Source', 'Hall1':'', 'Hall2':'', 'Hall3':'X', 'Study':'1c StudyID'},
        'Talk 2c':{'Speaker':'2c ID', 'Talk': '2', 'Assistant':'2c Assist ID', 'Source':'2 Source', 'Theme':'2 Theme', 'Hall1':'', 'Hall2':'', 'Hall3':'X', 'Study':'2c StudyID'},
        'Talk 3c':{'Speaker':'3c ID', 'Talk': '3', 'Assistant':'3c Assist ID', 'Source':'3 Source', 'Theme':'3 Theme', 'Hall1':'', 'Hall2':'', 'Hall3':'X', 'Study':'3c StudyID'}}

    info = {'Name':''}
    name = Tms.getSpeakerName(self, date, talks[talk]['Speaker'])
    if name != '':
        info = {'Name':name,
            'Talk':talks[talk]['Talk'],
            'Date':date,
            'Assistant':Tms.getSpeakerName(self, date, talks[talk].get('Assistant','')),
            'Source': Tms.getSourceMaterial(self, date, talks[talk]['Source']),
            'Theme': Tms.getSourceMaterial(self, date, talks[talk].get('Theme','')),
            'Setting':'',
            'Hall1': talks[talk]['Hall1'],
            'Hall2':talks[talk]['Hall2'],
            'Hall3':talks[talk]['Hall3'],
            'Study':Tms.getStudyNumber(self, date, talks[talk].get('Study',''), words=False)}
    return info

def drawAssignmentSlip(c, info):
    startY = -10*inch
    startX = 3.05*inch
    c.rotate(270)

    setFontSize(c, 'Name', info)
    c.drawString(startY, startX, info['Name'])
    c.setFont(myFont, myFontSize)
    c.drawString(startY + (3.2*inch), startX, info['Talk'])
    c.drawString(startY + (3.85*inch), startX,info['Date'])

    setFontSize(c, 'Assistant', info)
    c.drawString(startY + (.15*inch), startX - (.35*inch), info['Assistant'])
    setFontSize(c, 'Source', info)
    c.drawString(startY + (3.1*inch), startX - (.35*inch), info['Source'])

    setFontSize(c, 'Theme', info)
    c.drawString(startY, startX - (.7*inch),info['Theme'])

    c.setFont(myFont, myFontSize)
    c.drawString(startY, startX - (1.04*inch), info['Setting'])
    c.drawString(startY + (1.75*inch), startX - (1.08*inch), info['Hall1'])
    c.drawString(startY + (2.65*inch), startX - (1.08*inch), info['Hall2'])
    c.drawString(startY + (3.67*inch), startX - (1.08*inch), info['Hall3'])

    c.drawString(startY + (2.5*inch), startX - (1.5*inch), info['Study'])
    c.showPage()

def getNextWeek(date):
    wxDate = Tms.convertGrid2wxDate(date)
    dt = wxDate.AddDS(wx.DateSpan.Days(6))
    nextWeek = Tms.convertWx2Display(dt)
    return nextWeek

class Printer:
    def __init__(self, Parent, DocName):
        self.prnt = Parent
        self.DocName = tempfile.mktemp('.pdf')
        self.doc = SimpleDocTemplate(self.DocName)
        PAGE_WIDTH, PAGE_HEIGHT = letter
        styles = getSampleStyleSheet()
        self.style = styles["Normal"]
        self.styleH1 = styles['Heading1']
        self.styleH2 = styles['Heading2']
        self.styleH3 = styles['Heading3']
        self.Title = styles['Title']

    def doPrint(self,story):
        PdfReader = self.p.db.Options.get('Pdf Reader','')
        if PdfReader == '':
            dlg = wx.MessageBox("You don\'t have a PDF reader installed, so you can\'t print reports!", "No PDF Reader Installed", wx.OK)
            return
        self.doc.build(story)
        subprocess.call([PdfReader, self.DocName])
        try:
            os.remove(self.DocName)
        except:
            pass

def getPastRemarks(self, speaker):
    #get past 3 comments about speaker to post on worksheet
    comments = []
    if speaker in self.p.db.TalkHistory:
        talks = self.p.db.TalkHistory[speaker]
        for talk in talks:
            if talk.get('Remarks','') != '':
                comments.append(Tms.getDisplayDate(talk['Date']) + ': ' + talk['Remarks'])
    #if len(comments) >2:
    comments = comments[-2:]
    return comments

def buildWorksheets(self, repDates):
    #build worksheet canvas
    PdfReader = self.p.db.Options.get('Pdf Reader','')
    fname = tempfile.mktemp('.pdf')
    c = canvas.Canvas(fname, pagesize=letter, bottomup=0)

    for date in repDates:
        review = False
        if self.p.db.SourceMaterial[date].get('Review','N') == 'Y':
            review = True
        c.setFont('Times-Bold', 18)
        c.drawCentredString(x=4.25*inch, y=.7*inch, text="Worksheet for MainSchool  -  Week of " + date)

        c.setFont(myFont, 12)
        c.drawString(x=.5*inch, y=1*inch, text='School Begins: 8:00pm')

        #Bible Highlights
        c.setFont('Times-Bold', 12)
        c.drawString(x=.5*inch, y=1.3*inch, text='Bible Highlights')
        c.setFont(myFont, 12)
        c.drawString(x=2*inch, y=1.3*inch, text='Speaker: ' + Tms.getSpeakerName(self, date, 'H ID') + '      Substitute:__________________')
        c.drawString(x=.5*inch, y=1.5*inch, text = 'Source Material: ' + Tms.getSourceMaterial(self, date, 'H Source'))
        X = .5*inch
        Y= 1.7*inch
        remarks = getPastRemarks(self, self.p.db.Calendar[date].get('H ID', -1))
        if len(remarks) >0:
            c.setFont(myFont, 10)
            for remark in remarks:
                c.drawString(X,Y, remark)
                Y += .15*inch
        Y+= .05*inch
        c.setFont(myFont, 12)
        c.drawString(X,Y,'Notes:')
        c.drawString(X, 3.04*inch, 'Timing:_________')
        c.rect(.45*inch, 1.1*inch, 7.5*inch, 2*inch)

        #draw rest of talks if no review
        if review == True:
            c.setFont('Times-Bold', 16)
            c.drawCentredString(4.25*inch, 4*inch, 'Theocratic Ministry School Review')
        else:
            #Talk 1 info
            c.setFont('Times-Bold', 12)
            X=.5*inch
            Y= 3.4*inch
            c.drawString(X, Y, text='Talk 1')
            c.setFont(myFont, 12)
            c.drawString(X+1*inch, Y, 'Student: ' + Tms.getSpeakerName(self, date, '1a ID') + '      Substitute:__________________')
            Y += .2*inch
            c.drawString(X, Y, 'Source Material: ' + Tms.getSourceMaterial(self, date, '1 Source'))
            Y += .2*inch
            c.drawString(X, Y, 'Working on: ' + Tms.getStudyTitle(self, date, '1a StudyID'))
            Y += .2*inch
            remarks = getPastRemarks(self, self.p.db.Calendar[date].get('1a ID', -1))
            if len(remarks) >0:
                c.setFont(myFont, 10)
                for remark in remarks:
                    c.drawString(X,Y, remark)
                    Y += .15*inch
            Y+= .05*inch
            c.setFont(myFont, 12)
            c.drawString(X,Y,'Notes:')
            c.drawString(X, 5.14*inch, 'Timing:_________')
            c.drawString(5*inch, 5.14*inch, 'Move Student on to next Study point? ____')
            c.rect(.45*inch, 3.2*inch, 7.5*inch, 2*inch)

            #Talk 2
            c.setFont('Times-Bold', 12)
            X=.5*inch
            Y= 5.45*inch
            c.drawString(X, Y, text='Talk 2')
            c.setFont(myFont, 12)
            c.drawString(X+1*inch, Y, 'Student: ' + Tms.getSpeakerName(self, date, '2a ID') + '      Substitute:__________________')
            Y += .2*inch
            c.drawString(X+1*inch, Y, 'Assistant: ' + Tms.getSpeakerName(self, date, '2a Assist ID') + '      Substitute:__________________')
            Y += .2*inch
            info = {'wSource':'Source Material: ' + Tms.getSourceMaterial(self, date, '2 Source') + ' ' + Tms.getSourceMaterial(self, date, '2 Theme')}
            setFontSize(c, 'wSource', info)
            c.drawString(X, Y, info['wSource'])
            Y += .2*inch
            c.drawString(X,Y, 'Setting Used (from BE bk):______________________________________________________________')
            Y += .2*inch
            c.drawString(X, Y, 'Working on: ' + Tms.getStudyTitle(self, date, '2a StudyID'))
            Y += .2*inch
            remarks = getPastRemarks(self, self.p.db.Calendar[date].get('2a ID', -1))
            if len(remarks) >0:
                c.setFont(myFont, 10)
                for remark in remarks:
                    c.drawString(X,Y, remark)
                    Y += .15*inch
                Y+= .05*inch
            c.setFont(myFont, 12)
            c.drawString(X,Y,'Notes:')
            c.drawString(X, 7.24*inch, 'Timing:_________')
            c.drawString(5*inch, 7.24*inch, 'Move Student on to next Study point? ____')
            c.rect(.45*inch, 5.3*inch, 7.5*inch, 2*inch)

            #Talk 3
            female = getStudentsSex(self, date, '3a ID')
            c.setFont('Times-Bold', 12)
            X=.5*inch
            Y= 7.55*inch
            c.drawString(X, Y, text='Talk 3')
            c.setFont(myFont, 12)
            c.drawString(X+1*inch, Y, 'Student: ' + Tms.getSpeakerName(self, date, '3a ID') + '      Substitute:__________________')
            Y += .2*inch
            if female == True:
                c.drawString(X+1*inch, Y, 'Assistant: ' + Tms.getSpeakerName(self, date, '3a Assist ID') + '      Substitute:__________________')
                Y += .2*inch
            info = {'wSource':'Source Material: ' + Tms.getSourceMaterial(self, date, '3 Source') + ' ' + Tms.getSourceMaterial(self, date, '3 Theme')}
            setFontSize(c, 'wSource', info)
            c.drawString(X, Y, info['wSource'])
            Y += .2*inch
            if female == True:
                c.drawString(X,Y, 'Setting Used (from BE bk):______________________________________________________________')
                Y += .2*inch
            c.drawString(X, Y, 'Working on: ' + Tms.getStudyTitle(self, date, '3a StudyID'))
            Y += .2*inch
            remarks = getPastRemarks(self, self.p.db.Calendar[date].get('3a ID', -1))
            if len(remarks) >0:
                c.setFont(myFont, 10)
                for remark in remarks:
                    c.drawString(X,Y, remark)
                    Y += .15*inch
                Y+= .05*inch
            c.setFont(myFont, 12)
            c.drawString(X,Y,'Notes:')
            c.drawString(X, 9.34*inch, 'Timing:_________')
            c.drawString(5*inch, 9.34*inch, 'Move Student on to next Study point? ____')
            c.rect(.45*inch, 7.4*inch, 7.5*inch, 2*inch)

        #draw bottom of page
        c.drawString(x=.5*inch, y=9.7*inch, text='School Ends: 8:30pm')
        c.setFont(myFont, 10)
        nextWeek = getNextWeek(date)
        c.drawString(x=.5*inch, y=9.95*inch, text = 'Next Weeks Students:  Bible Highlights - ' + Tms.getSpeakerName(self, nextWeek, 'H ID'))
        if nextWeek in self.p.db.SourceMaterial:
            if self.p.db.SourceMaterial[nextWeek].get('Review', 'N') == 'Y':
                c.setFont('Times-Bold', 12)
                c.drawString(x=.5*inch, y=10.2*inch, text = 'Theocratic Ministry School Review')
            else: #print next weeks students
                c.setFont(myFont, 10)
                c.drawString(x=.5*inch, y=10.1*inch, text='1a: ' + Tms.getSpeakerName(self, nextWeek, '1a ID') + '     2a: ' + Tms.getSpeakerName(self, nextWeek, '2a ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '2a Assist ID') + '     3a: ' + Tms.getSpeakerName(self, nextWeek, '3a ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '3a Assist ID'))
                c.drawString(x=.5*inch, y=10.25*inch, text='1b: ' + Tms.getSpeakerName(self, nextWeek, '1b ID') + '     2b: ' + Tms.getSpeakerName(self, nextWeek, '2b ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '2b Assist ID') + '     3b: ' + Tms.getSpeakerName(self, nextWeek, '3b ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '3b Assist ID'))
        #Get name for person who introduces service meeting
        if self.p.db.Calendar[date].get('CO Visit', False) == True:
            #if co's visit then part 1 is skipped
            part = 'SM Part2 ID'
        else:
            part = 'SM Part1 ID'

        c.setFont('Times-Bold', 12)
        c.drawCentredString(x=4.25*inch, y=10.5*inch, text = 'Next Part - Service Meeting: ' + Tms.getSpeakerName(self, date, part))
        c.showPage()

        #now draw timing worksheet
        if not review:
            c.setFont('Times-Bold', 18)
            c.drawCentredString(x=4.25*inch, y=.7*inch, text="Timekeepers sheet for MainSchool  -  Week of " + date)

            c.setFont(myFont, 12)
            c.drawString(x=.5*inch, y=1*inch, text='School Begins: 8:00pm')

            #Bible Highlights
            c.setFont('Times-Bold', 12)
            c.drawString(x=.5*inch, y=1.3*inch, text='Bible Highlights')
            c.setFont(myFont, 12)
            c.drawString(x=2*inch, y=1.3*inch, text='Speaker: ' + Tms.getSpeakerName(self, date, 'H ID') + '      Substitute:__________________')
            c.drawString(x=.5*inch, y=1.5*inch, text = 'Source Material: ' + Tms.getSourceMaterial(self, date, 'H Source'))
            X = .5*inch
            Y= 1.7*inch
            c.setFont(myFont, 12)
            c.drawString(X,Y,'Notes:')
            c.drawString(X, 3.04*inch, 'Timing:_________')
            c.rect(.45*inch, 1.1*inch, 7.5*inch, 2*inch)

            #Talk 1 info
            c.setFont('Times-Bold', 12)
            X=.5*inch
            Y= 3.4*inch
            c.drawString(X, Y, text='Talk 1')
            c.setFont(myFont, 12)
            c.drawString(X+1*inch, Y, 'Student: ' + Tms.getSpeakerName(self, date, '1a ID') + '      Substitute:__________________')
            Y += .2*inch
            c.drawString(X, Y, 'Source Material: ' + Tms.getSourceMaterial(self, date, '1 Source'))
            Y += .2*inch
            c.setFont(myFont, 12)
            c.drawString(X,Y,'Notes:')
            c.drawString(X, 5.14*inch, 'Timing:_________')
            c.rect(.45*inch, 3.2*inch, 7.5*inch, 2*inch)

            #Talk 2
            c.setFont('Times-Bold', 12)
            X=.5*inch
            Y= 5.45*inch
            c.drawString(X, Y, text='Talk 2')
            c.setFont(myFont, 12)
            c.drawString(X+1*inch, Y, 'Student: ' + Tms.getSpeakerName(self, date, '2a ID') + '      Substitute:__________________')
            Y += .2*inch
            c.drawString(X+1*inch, Y, 'Assistant: ' + Tms.getSpeakerName(self, date, '2a Assist ID') + '      Substitute:__________________')
            Y += .2*inch
            info = {'wSource':'Source Material: ' + Tms.getSourceMaterial(self, date, '2 Source') + ' ' + Tms.getSourceMaterial(self, date, '2 Theme')}
            setFontSize(c, 'wSource', info)
            c.drawString(X, Y, info['wSource'])
            Y += .2*inch
            c.drawString(X,Y, 'Setting Used (from BE bk):______________________________________________________________')
            Y += .2*inch
            c.setFont(myFont, 12)
            c.drawString(X,Y,'Notes:')
            c.drawString(X, 7.24*inch, 'Timing:_________')
            c.rect(.45*inch, 5.3*inch, 7.5*inch, 2*inch)

            #Talk 3
            female = getStudentsSex(self, date, '3a ID')
            c.setFont('Times-Bold', 12)
            X=.5*inch
            Y= 7.55*inch
            c.drawString(X, Y, text='Talk 3')
            c.setFont(myFont, 12)
            c.drawString(X+1*inch, Y, 'Student: ' + Tms.getSpeakerName(self, date, '3a ID') + '      Substitute:__________________')
            Y += .2*inch
            if female == True:
                c.drawString(X+1*inch, Y, 'Assistant: ' + Tms.getSpeakerName(self, date, '3a Assist ID') + '      Substitute:__________________')
                Y += .2*inch
            info = {'wSource':'Source Material: ' + Tms.getSourceMaterial(self, date, '3 Source') + ' ' + Tms.getSourceMaterial(self, date, '3 Theme')}
            setFontSize(c, 'wSource', info)
            c.drawString(X, Y, info['wSource'])
            Y += .2*inch
            if female == True:
                c.drawString(X,Y, 'Setting Used (from BE bk):______________________________________________________________')
                Y += .2*inch
            c.setFont(myFont, 12)
            c.drawString(X,Y,'Notes:')
            c.drawString(X, 9.34*inch, 'Timing:_________')
            c.rect(.45*inch, 7.4*inch, 7.5*inch, 2*inch)

            #draw bottom of page
            c.drawString(x=.5*inch, y=9.7*inch, text='School Ends: 8:30pm')
            c.setFont(myFont, 10)
            nextWeek = getNextWeek(date)
            c.drawString(x=.5*inch, y=9.95*inch, text = 'Next Weeks Students:  Bible Highlights - ' + Tms.getSpeakerName(self, nextWeek, 'H ID'))
            if nextWeek in self.p.db.SourceMaterial:
                if self.p.db.SourceMaterial[nextWeek].get('Review', 'N') == 'Y':
                    c.setFont('Times-Bold', 12)
                    c.drawString(x=.5*inch, y=10.2*inch, text = 'Theocratic Ministry School Review')
                else: #print next weeks students
                    c.setFont(myFont, 10)
                    c.drawString(x=.5*inch, y=10.1*inch, text='1a: ' + Tms.getSpeakerName(self, nextWeek, '1a ID') + '     2a: ' + Tms.getSpeakerName(self, nextWeek, '2a ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '2a Assist ID') + '     3a: ' + Tms.getSpeakerName(self, nextWeek, '3a ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '3a Assist ID'))
                    c.drawString(x=.5*inch, y=10.25*inch, text='1b: ' + Tms.getSpeakerName(self, nextWeek, '1b ID') + '     2b: ' + Tms.getSpeakerName(self, nextWeek, '2b ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '2b Assist ID') + '     3b: ' + Tms.getSpeakerName(self, nextWeek, '3b ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '3b Assist ID'))
            #Get name for person who introduces service meeting
            if self.p.db.Calendar[date].get('CO Visit', False) == True:
                #if co's visit then part 1 is skipped
                part = 'SM Part2 ID'
            else:
                part = 'SM Part1 ID'

            c.setFont('Times-Bold', 12)
            c.drawCentredString(x=4.25*inch, y=10.5*inch, text = 'Next Part - Service Meeting: ' + Tms.getSpeakerName(self, date, part))
            c.showPage()

            # now draw 2nd school
            if self.p.db.Calendar[date].get('#Schools',2) > 1:
                c.setFont('Times-Bold', 18)
                c.drawCentredString(x=4.25*inch, y=.7*inch, text="Worksheet for 2nd School  -  Week of " + date)

                c.setFont(myFont, 12)
                c.drawString(x=.5*inch, y=1*inch, text='School Begins: 8:00pm')

                #Talk 1 info
                c.setFont('Times-Bold', 12)
                X = t1X = .5*inch
                Y = t1Y = 1.3*inch
                c.drawString(X, Y, text='Talk 1')
                c.setFont(myFont, 12)
                c.drawString(X+1*inch, Y, 'Student: ' + Tms.getSpeakerName(self, date, '1b ID') + '      Substitute:__________________')
                Y += .2*inch
                c.drawString(X, Y, 'Source Material: ' + Tms.getSourceMaterial(self, date, '1 Source'))
                Y += .2*inch
                c.drawString(X, Y, 'Working on: ' + Tms.getStudyTitle(self, date, '1b StudyID'))
                Y += .2*inch
                remarks = getPastRemarks(self, self.p.db.Calendar[date].get('1b ID', -1))
                if len(remarks) >0:
                    c.setFont(myFont, 10)
                    for remark in remarks:
                        c.drawString(X,Y, remark)
                        Y += .15*inch
                Y+= .05*inch
                c.setFont(myFont, 12)
                c.drawString(X,Y,'Notes:')
                c.drawString(t1X, t1Y +2.24*inch, 'Timing:_________')
                c.drawString(t1X + 4.5*inch, t1Y +2.24*inch, 'Move Student on to next Study point? ____')
                c.rect(t1X - .05*inch, t1Y - .2*inch, 7.5*inch, 2.5*inch)

                #Talk 2
                c.setFont('Times-Bold', 12)
                X= t2X = .5*inch
                Y= t2Y = 4.2*inch
                c.drawString(X, Y, text='Talk 2')
                c.setFont(myFont, 12)
                c.drawString(X+1*inch, Y, 'Student: ' + Tms.getSpeakerName(self, date, '2b ID') + '      Substitute:__________________')
                Y += .2*inch
                c.drawString(X+1*inch, Y, 'Assistant: ' + Tms.getSpeakerName(self, date, '2b Assist ID') + '      Substitute:__________________')
                Y += .2*inch
                info = {'wSource':'Source Material: ' + Tms.getSourceMaterial(self, date, '2 Source') + ' ' + Tms.getSourceMaterial(self, date, '2 Theme')}
                setFontSize(c, 'wSource', info)
                c.drawString(X, Y, info['wSource'])
                Y += .2*inch
                c.drawString(X,Y, 'Setting Used (from BE bk):______________________________________________________________')
                Y += .2*inch
                c.drawString(X, Y, 'Working on: ' + Tms.getStudyTitle(self, date, '2b StudyID'))
                Y += .2*inch
                remarks = getPastRemarks(self, self.p.db.Calendar[date].get('2b ID', -1))
                if len(remarks) >0:
                    c.setFont(myFont, 10)
                    for remark in remarks:
                        c.drawString(X,Y, remark)
                        Y += .15*inch
                    Y+= .05*inch
                c.setFont(myFont, 12)
                c.drawString(X,Y,'Notes:')
                c.drawString(X, t2Y +2.24*inch, 'Timing:_________')
                c.drawString(5*inch, t2Y +2.24*inch, 'Move Student on to next Study point? ____')
                c.rect(t2X - .05*inch, t2Y - .2*inch, 7.5*inch, 2.5*inch)

                #Talk 3
                female = getStudentsSex(self, date, '3b ID')
                c.setFont('Times-Bold', 12)
                X= t3X = .5*inch
                Y= t3Y = 7.05*inch
                c.drawString(X, Y, text='Talk 3')
                c.setFont(myFont, 12)
                c.drawString(X+1*inch, Y, 'Student: ' + Tms.getSpeakerName(self, date, '3b ID') + '      Substitute:__________________')
                Y += .2*inch
                if female == True:
                    c.drawString(X+1*inch, Y, 'Assistant: ' + Tms.getSpeakerName(self, date, '3b Assist ID') + '      Substitute:__________________')
                    Y += .2*inch
                info = {'wSource':'Source Material: ' + Tms.getSourceMaterial(self, date, '3 Source') + ' ' + Tms.getSourceMaterial(self, date, '3 Theme')}
                setFontSize(c, 'wSource', info)
                c.drawString(X, Y, info['wSource'])
                Y += .2*inch
                if female == True:
                    c.drawString(X,Y, 'Setting Used (from BE bk):______________________________________________________________')
                    Y += .2*inch
                c.drawString(X, Y, 'Working on: ' + Tms.getStudyTitle(self, date, '3b StudyID'))
                Y += .2*inch
                remarks = getPastRemarks(self, self.p.db.Calendar[date].get('3b ID', -1))
                if len(remarks) >0:
                    c.setFont(myFont, 10)
                    for remark in remarks:
                        c.drawString(X,Y, remark)
                        Y += .15*inch
                    Y+= .05*inch
                c.setFont(myFont, 12)
                c.drawString(X,Y,'Notes:')
                c.drawString(X, t3Y +2.24*inch, 'Timing:_________')
                c.drawString(5*inch, t3Y +2.24*inch, 'Move Student on to next Study point? ____')
                c.rect(t3X - .05*inch, t3Y - .2*inch, 7.5*inch, 2.5*inch)

                #draw bottom of page
                c.drawString(x=.5*inch, y=9.7*inch, text='School Ends: 8:30pm')
                c.setFont(myFont, 10)
                nextWeek = getNextWeek(date)
                c.drawString(x=.5*inch, y=9.95*inch, text = 'Next Weeks Students:  Bible Highlights - ' + Tms.getSpeakerName(self, nextWeek, 'H ID'))
                if nextWeek in self.p.db.SourceMaterial:
                    if self.p.db.SourceMaterial[nextWeek].get('Review', 'N') == 'Y':
                        c.setFont('Times-Bold', 12)
                        c.drawString(x=.5*inch, y=10.2*inch, text = 'Theocratic Ministry School Review')
                    else: #print next weeks students
                        c.setFont(myFont, 10)
                        c.drawString(x=.5*inch, y=10.1*inch, text='1a: ' + Tms.getSpeakerName(self, nextWeek, '1a ID') + '     2a: ' + Tms.getSpeakerName(self, nextWeek, '2a ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '2a Assist ID') + '     3a: ' + Tms.getSpeakerName(self, nextWeek, '3a ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '3a Assist ID'))
                        c.drawString(x=.5*inch, y=10.25*inch, text='1b: ' + Tms.getSpeakerName(self, nextWeek, '1b ID') + '     2b: ' + Tms.getSpeakerName(self, nextWeek, '2b ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '2b Assist ID') + '     3b: ' + Tms.getSpeakerName(self, nextWeek, '3b ID') + ' \\ ' + Tms.getSpeakerName(self, nextWeek, '3b Assist ID'))
                #Get name for person who introduces service meeting
                if self.p.db.Calendar[date].get('CO Visit', False) == True:
                    #if co's visit then part 1 is skipped
                    part = 'SM Part2 ID'
                else:
                    part = 'SM Part1 ID'

                c.setFont('Times-Bold', 12)
                c.drawCentredString(x=4.25*inch, y=10.5*inch, text = 'Next Part - Service Meeting: ' + Tms.getSpeakerName(self, date, part))
                c.showPage()



    c.save()

    subprocess.call([PdfReader, fname])
    try:
        os.remove(fname)
    except:
        pass

def drawScheduleTitle(self, info):
    X=info['startX']
    Y=info['startY']
    c=info['pg']
    if 'meeting' in info:
        X += .25*inch
    if info['Title'] == 'Service Meeting':
        if info['coVisit'] == False:
            song = getScheduleSong(self, info['date'], 'SM1')
            c.setFillColor(colors.black)
            c.drawString(X, Y, '8:30pm   Song ' + song)
            Y += .25*inch
    elif info['Title'] == 'Watchtower':
        song = getScheduleSong(self, info['date'], 'WT1')
        c.setFillColor(colors.black)
        c.drawString(X, Y, '10:35am   Song ' + song)
        Y += .25*inch
    elif info['Title'] == 'Circuit Overseers Talk':
        if info['meeting'] == 'CO Talk 1':
            c.setFillColor(colors.black)
            c.drawString(X, Y, '8:35pm   Song ____')
            Y += .25*inch

    c.setFillColor(info['colorBackground'])
    width = c.stringWidth(info['Title'], 'Times-Bold', 12)
    c.rect(X-.05*inch,Y-.15*inch, width + .1*inch, .2*inch, stroke=0, fill=1)
    c.setFont('Times-Bold', 12)
    c.setFillColor(info['colorFont'])
    c.drawString(X, Y, info['Title'])
    if 'Title' in info:
        c.setFillColor(colors.black)
        if info['Title'] == 'Tuesday':
            if info['coVisit'] == True:
                c.drawString(X+width+.05*inch, Y, ' - 7:30pm   Song ____ and Opening Prayer')
        elif info['Title'] == 'Thursday':
            song = getScheduleSong(self, info['date'], 'CBS')
            c.drawString(X+width+.05*inch, Y, ' - 7:30pm   Song ' + song + ' and Opening Prayer')
        elif info['Title'] == 'Sunday':
            c.drawString(X+width+.05*inch, Y, ' - 10:00am   Song ____ and Opening Prayer')
        elif info['Title'] == 'Theocratic Ministry School':
            if info['coVisit'] == True:
                c.drawString(X+width+.05*inch, Y, ' - 7:35pm')
            else:
                c.drawString(X+width+.05*inch, Y, ' - 8:00pm')
        elif info['Title'] == 'Congregation Bible Study':
            c.drawString(X+width+.05*inch, Y, ' - 7:35pm')
        elif info['Title'] == 'Service Meeting':
            if info['coVisit'] == True:
                c.drawString(X+width+.05*inch, Y, ' - 8:05pm')
            else:
                c.drawString(X+width+.05*inch, Y, ' - 8:35pm')
        elif info['Title'] == 'Public Talk':
            c.drawString(X+width+.05*inch, Y, ' - 10:05am')
        elif info['Title'] == 'Watchtower':
            c.drawString(X+width+.05*inch, Y, ' - 10:40am')
        elif info['Title'] == 'Circuit Overseers Talk':
            if info['meeting'] == 'CO Talk 1':
                c.drawString(X+width+.05*inch, Y, ' - 8:40pm')
            if info['meeting'] == 'CO Talk 2':
                c.drawString(X+width+.05*inch, Y, ' - 8:00pm')
            if info['meeting'] == 'CO Talk 3':
                c.drawString(X+width+.05*inch, Y, ' - 11:10am')
    Y+=.25*inch
    return Y

def getScheduleSong(self, date, meeting):
    meetings = {'CBS': 'CBS Song', 'SM1': 'SM Song1', 'SM2':'SM Song2', 'WT1': 'W Song1', 'WT2':'W Song2'}
    song = '____'
    if date in self.p.db.SourceMaterial:
        song = str(self.p.db.SourceMaterial[date].get(meetings[meeting],'____'))
    if (song == '0') or (song == ''):
        song = '____'
    return song

def drawMeeting(self, info):
    c = info['pg']
    X= info['startX']
    Y = info['startY']
    info['colorFont'] = colors.white
    info['colorBackground'] = colors.blue
    c.setFont(myFont, myFontSize)

    if info['meeting'] == 'CBS':
        info['Title'] = 'Congregation Bible Study'
        Y = drawScheduleTitle(self, info)
        c.setFillColor(colors.black)
        c.setFont(myFont, myFontSize)

        lines = [['Conductor: ', myFont, myFontSize, False]]
        lines.append([Tms.getSpeakerName(self, info['date'], 'CBS Conductor'), myFont, myFontSize, True])
        lines.append(['   |   Source: ' + Tms.getSourceMaterial(self, info['date'], 'CBS Source'), myFont, myFontSize, False])
        parts = {'canvas': c, 'startX':X+.25*inch, 'startY': Y, 'lines': lines}
        drawLineEffects(parts)
        Y += .2*inch

    elif info['meeting'] == 'TMS':
        info['Title'] = 'Theocratic Ministry School'
        Y = drawScheduleTitle(self, info)

        lines = [['Bible Highlights: ', myBoldFont, myFontSize, False]]
        lines.append([Tms.getSpeakerName(self, info['date'], 'H ID'), myFont, myFontSize, True])
        lines.append(['     Source: ' + Tms.getSourceMaterial(self, info['date'], 'H Source'), myFont, myFontSize, False])
        parts = {'canvas': c, 'startX':X+.35*inch, 'startY': Y, 'lines': lines}
        drawLineEffects(parts)
        Y += .2*inch

        # draw school info only if not a review night
        if self.p.db.SourceMaterial[info['date']].get('Review','N') == 'N':
            lines = [['Talk 1: ', myBoldFont, myFontSize, False]]
            lines.append(['Source: ' + Tms.getSourceMaterial(self, info['date'], '1 Source'), myFont, myFontSize, False])
            parts = {'canvas': c, 'startX':X+.35*inch, 'startY': Y, 'lines': lines}
            drawLineEffects(parts)
            Y += .2*inch

            lines = [['Main School:   ', myFont, myFontSize, False]]
            lines.append([Tms.getSpeakerName(self, info['date'], '1a ID'), myFont, myFontSize, True])
            lines.append(['  --> ' + Tms.getStudyNumber(self, info['date'], '1a StudyID'), myFont, myFontSize, False])
            parts = {'canvas': c, 'startX':X+.6*inch, 'startY': Y, 'lines': lines}
            drawLineEffects(parts)
            c.line(X+.50*inch,Y-.17*inch, X+.50*inch, Y-.05*inch)
            c.line(X+.50*inch, Y-.05*inch, X+.6*inch, Y-.05*inch)
            Y += .2*inch
 
            if isSecondSchool(self, info['date']) == True:
                lines = [['2nd School:   ', myFont, myFontSize, False]]
                lines.append([Tms.getSpeakerName(self, info['date'], '1b ID'), myFont, myFontSize, True])
                lines.append(['  --> ' + Tms.getStudyNumber(self, info['date'], '1b StudyID'), myFont, myFontSize, False])
                parts = {'canvas': c, 'startX':X+.6*inch, 'startY': Y, 'lines': lines}
                drawLineEffects(parts)

                c.line(X+.50*inch,Y-.25*inch, X+.50*inch, Y-.05*inch)
                c.line(X+.50*inch, Y-.05*inch, X+.6*inch, Y-.05*inch)
                Y += .2*inch

            lines = [['Talk 2: ', myBoldFont, myFontSize, False]]
            lines.append(['Source: ' + Tms.getSourceMaterial(self, info['date'], '2 Source') + '  ' + Tms.getSourceMaterial(self, info['date'], '2 Theme'), myFont, myFontSize, False])
            parts = {'canvas': c, 'startX':X+.35*inch, 'startY': Y, 'lines': lines}
            drawLineEffects(parts)
            Y += .2*inch

            lines = [['Main School:   ', myFont, myFontSize, False]]
            lines.append([Tms.getSpeakerName(self, info['date'], '2a ID'), myFont, myFontSize, True])
            lines.append(['  --> ' + Tms.getStudyNumber(self, info['date'], '2a StudyID') + '   Assistant: ', myFont, myFontSize, False])
            lines.append([Tms.getSpeakerName(self, info['date'], '2a Assist ID'), myFont, myFontSize, True])
            parts = {'canvas': c, 'startX':X+.6*inch, 'startY': Y, 'lines': lines}
            drawLineEffects(parts)
            c.line(X+.50*inch,Y-.17*inch, X+.50*inch, Y-.05*inch)
            c.line(X+.50*inch, Y-.05*inch, X+.6*inch, Y-.05*inch)
            Y += .2*inch

            if isSecondSchool(self, info['date']) == True:
                lines = [['2nd School:   ', myFont, myFontSize, False]]
                lines.append([Tms.getSpeakerName(self, info['date'], '2b ID'), myFont, myFontSize, True])
                lines.append(['  --> ' + Tms.getStudyNumber(self, info['date'], '2b StudyID') + '   Assistant: ', myFont, myFontSize, False])
                lines.append([Tms.getSpeakerName(self, info['date'], '2b Assist ID'), myFont, myFontSize, True])
                parts = {'canvas': c, 'startX':X+.6*inch, 'startY': Y, 'lines': lines}
                drawLineEffects(parts)
                c.line(X+.50*inch,Y-.25*inch, X+.50*inch, Y-.05*inch)
                c.line(X+.50*inch, Y-.05*inch, X+.6*inch, Y-.05*inch)
                Y += .2*inch

##            lines = [['Talk 3: ', myBoldFont, myFontSize, False]]
##            lines.append(['Source: ' + Tms.getSourceMaterial(self, info['date'], '3 Source') + '  ' + Tms.getSourceMaterial(self, info['date'], '3 Theme'), myFont, myFontSize, False])
##            parts = {'canvas': c, 'startX':X+.35*inch, 'startY': Y, 'lines': lines}
##            drawLineEffects(parts)

            lineX = X+.35*inch
            part1 = 'Talk 3:'
            part2 = 'Source: '
            part3 = '"'+Tms.getSourceMaterial(self, info['date'], '3 Source') + '  ' + Tms.getSourceMaterial(self, info['date'], '3 Theme')+'"'
            c.setFont(myBoldFont, myFontSize)
            part1Width = c.stringWidth(part1, myBoldFont, myFontSize)
            part2Width = c.stringWidth(part2, myFont, myFontSize)
            part3Width = c.stringWidth(part3, myFont, myFontSize)
            c.drawString(lineX, Y, part1)
            lineX += part1Width + .1*inch
            c.setFont(myFont, myFontSize)
            c.drawString(lineX, Y, part2)
            lineX += part2Width
            maxWidth = 8*inch - lineX
            big = False
            part3a = part3
            part3b = ''
            # breaks string into another line if too big for 1 line
            while part3Width > maxWidth:
                big = True
                part3b = part3a[part3a.rfind(' '):] + part3b
                part3a = part3a[:part3a.rfind(' ')]
                part3Width = c.stringWidth(part3a, myFont, myFontSize)
            part3 = part3a
            c.drawString(lineX, Y, part3)
            if big:
                Y += .2*inch
                #lineX += .2*inch
                c.drawString(lineX, Y, part3b)
            #c.drawString(X+.35*inch, Y, 'Talk 3:    Source: ' + Tms.getSourceMaterial(self, info['date'], '3 Source') + '  ' + Tms.getSourceMaterial(self, info['date'], '3 Theme'))
            Y += .2*inch

            lineX = X+.6*inch
            part1 = 'Main School:   '
            part2 = Tms.getSpeakerName(self, info['date'], '3a ID')
            part3 = '  --> '
            part4 = Tms.getStudyNumber(self, info['date'], '3a StudyID')
            # show assistant only if student is female
            part5 = ''
            if getStudentsSex(self, info['date'], '3a ID') == True:
                part5 = '   Assistant: ' + Tms.getSpeakerName(self, info['date'], '3a Assist ID')
            part1Width = c.stringWidth(part1, myFont, myFontSize)
            part2Width = c.stringWidth(part2, myFont, myFontSize)
            part3Width = c.stringWidth(part3, myFont, myFontSize)
            part4Width = c.stringWidth(part4, myFont, myFontSize)
            part5Width = c.stringWidth(part5, myFont, myFontSize)
            c.setFont(myFont, myFontSize)
            c.drawString(lineX, Y, part1)
            lineX += part1Width
            c.drawString(lineX, Y, part2)
            c.line(lineX, Y+.03*inch, lineX+part2Width, Y+.03*inch)
            lineX += part2Width
            c.drawString(lineX, Y, part3)
            lineX += part3Width
            c.drawString(lineX, Y, part4)
            lineX += part4Width
            c.drawString(lineX, Y, part5)
            c.line(lineX, Y+.03*inch, lineX+part5Width, Y+.03*inch)
            if big:
                Y1 = Y - .35*inch
            else:
                Y1 = Y - .17*inch
            c.line(X+.50*inch, Y1, X+.50*inch, Y-.05*inch)
            c.line(X+.50*inch, Y-.05*inch, X+.6*inch, Y-.05*inch)
            Y += .2*inch
            if isSecondSchool(self, info['date']) == True:
                lineX = X+.6*inch
                part1 = '2nd School:   '
                part2 = Tms.getSpeakerName(self, info['date'], '3b ID')
                part3 = '  --> '
                part4 = Tms.getStudyNumber(self, info['date'], '3b StudyID')
                # show assistant only if student is female
                part5 = ''
                if getStudentsSex(self, info['date'], '3b ID') == True:
                    part5 = '   Assistant: ' + Tms.getSpeakerName(self, info['date'], '3b Assist ID')
                part1Width = c.stringWidth(part1, myFont, myFontSize)
                part2Width = c.stringWidth(part2, myFont, myFontSize)
                part3Width = c.stringWidth(part3, myFont, myFontSize)
                part4Width = c.stringWidth(part4, myFont, myFontSize)
                part5Width = c.stringWidth(part5, myFont, myFontSize)
                c.setFont(myFont, myFontSize)
                c.drawString(lineX, Y, part1)
                lineX += part1Width
                c.drawString(lineX, Y, part2)
                c.line(lineX, Y+.03*inch, lineX+part2Width, Y+.03*inch)
                lineX += part2Width
                c.drawString(lineX, Y, part3)
                lineX += part3Width
                c.drawString(lineX, Y, part4)
                lineX += part4Width
                c.drawString(lineX, Y, part5)
                c.line(lineX, Y+.03*inch, lineX+part5Width, Y+.03*inch)
                #c.drawString(X+.6*inch, Y, '2nd School:   ' + Tms.getSpeakerName(self, info['date'], '3b ID') +'  --> ' + Tms.getStudyNumber(self, info['date'], '3b StudyID') + '   Assistant: ' + Tms.getSpeakerName(self, info['date'], '3b Assist ID'))
                c.line(X+.50*inch,Y-.25*inch, X+.50*inch, Y-.05*inch)
                c.line(X+.50*inch, Y-.05*inch, X+.6*inch, Y-.05*inch)
                Y += .2*inch
        # this is a review night so print reader name
        else:
            Y += .1*inch
            c.setFont(myBoldFont, 14)
            c.drawCentredString(X + 4*inch, Y, 'Theocratic Ministry School Review')
            Y += .2*inch
            lineX = X + .35*inch
            part1 = 'Reader: '
            part1Width = c.stringWidth(part1, myBoldFont, myFontSize)
            part2 = Tms.getSpeakerName(self, info['date'], 'Reader ID')
            part2Width = c.stringWidth(part2, myFont, myFontSize)

            c.setFont(myBoldFont, myFontSize)
            c.drawString(lineX, Y, part1)
            lineX += part1Width
            c.setFont(myFont, myFontSize)
            c.drawString(lineX, Y, part2)
            c.line(lineX, Y+.03*inch, lineX+part2Width, Y+.03*inch)
            Y += .2*inch

    elif info['meeting'] == 'SM':
        info['Title'] = 'Service Meeting'
        Y = drawScheduleTitle(self, info)
        for talk in range(1, 6):
            if Tms.getSpeakerName(self, info['date'], SmParts[talk]) != '':
                info['smPart'] = Tms.getSourceMaterial(self, info['date'], SmSource[talk]) + '     ' + Tms.getSpeakerName(self, info['date'], SmParts[talk])
                setFontSize(c, 'smPart', info)
                c.drawString(X+.35*inch, Y, info['smPart'])
                Y += .2*inch
        if info['coVisit'] == False:
            #add condluding song & Prayer
            c.setFont(myFont, myFontSize)
            c.drawString(X+.25*inch,Y, '9:10pm Song ' + getScheduleSong(self, info['date'], 'SM2') + ' and Concluding Prayer')
            Y +=.2*inch


    elif info['meeting'] == 'PT':
        info['Title'] = 'Public Talk'
        Y = drawScheduleTitle(self, info)
        c.setFont(myFont, myFontSize)
        text = 'Chairman: ' + Tms.getSpeakerName(self, info['date'], 'PT Chairman')
        if info['coVisit'] == False:
            text += '    Hospitality: ' + Tms.getCalendarInfo(self, info['date'], 'PT Hospitality')
        c.drawString(X+.25*inch,Y, text)
        Y+=.2*inch
        info['pTalk'] = 'Speaker: ' + Tms.getCalendarInfo(self, info['date'], 'PT Speaker') + ' (' + Tms.getCalendarInfo(self, info['date'], 'PT Congregation') + ') Theme: ' + Tms.getCalendarInfo(self, info['date'], 'PT Theme')
        setFontSize(c, 'pTalk', info)
        c.drawString(X+.25*inch, Y, info['pTalk'])
        Y+=.2*inch
        # Add away speakers
        for talk in range(1, 4):
            if Tms.getSpeakerName(self, info['date'], AwaySpeakers[talk]) != '':
                info['pTalk'] = 'Outgoing Speaker: ' + Tms.getSpeakerName(self, info['date'], AwaySpeakers[talk]) + ' To: ' + Tms.getCalendarInfo(self, info['date'], AwayCong[talk]) + ' Theme: ' + Tms.getCalendarInfo(self, info['date'], AwayTheme[talk])
                setFontSize(c, 'pTalk', info)
                c.drawString(X+.25*inch, Y, info['pTalk'])
                Y += .2*inch

    elif info['meeting'] == 'WT':
        info['Title'] = 'Watchtower'
        Y = drawScheduleTitle(self, info)
        c.setFont(myFont, myFontSize)
        if info['coVisit'] == False:
            c.drawString(X+.35*inch, Y, 'Reader: ' + Tms.getSpeakerName(self, info['date'], 'Wt Reader'))
            Y+=.2*inch
            c.drawString(X+.25*inch, Y, '11:40am Song ' + getScheduleSong(self, info['date'], 'WT2') + ' and Concluding Prayer')
            Y+=.2*inch

    elif info['meeting'] == 'CO Talk 1':
        info['Title'] = 'Circuit Overseers Talk'
        Y = drawScheduleTitle(self, info)
        c.setFont(myFont, myFontSize)
        c.drawString(X+.25*inch, Y, 'Song ____ and Concluding prayer')
        Y+=.2*inch

    elif info['meeting'] == 'CO Talk 2':
        info['Title'] = 'Circuit Overseers Talk'
        Y = drawScheduleTitle(self, info)
        c.setFont(myFont, myFontSize)
        c.drawString(X+.25*inch, Y, 'Song ____ and Concluding prayer')
        Y+=.2*inch

    elif info['meeting'] == 'CO Talk 3':
        info['Title'] = 'Circuit Overseers Talk'
        Y = drawScheduleTitle(self, info)
        c.setFont(myFont, myFontSize)
        c.drawString(X+.25*inch, Y, 'Song ____ and Concluding prayer')
        Y+=.2*inch
    Y += .25*inch
    return Y

def buildSchedule(self, dates):
    #build Schedule report
    #todo: add a field at bottom that shows when this schedule was created or updated
    PdfReader = self.p.db.Options.get('Pdf Reader','')
    fname = tempfile.mktemp('.pdf')
    c = canvas.Canvas(fname, pagesize=letter, bottomup=0)

    for date in dates:
        review = False
        if self.p.db.SourceMaterial[date].get('Review','N') == 'Y':
            review = True
        coVisit = self.p.db.Calendar[date].get('CO Visit', 'False')
        c.setFont('Times-Bold', 18)
        X=.5*inch
        Y=.5*inch
        c.drawCentredString(X+3.75*inch, Y, "Congregation Meeting Schedule for "+ date + ' - ' + getNextWeek(date))

        if coVisit == True:
            Y += .25*inch
            c.setFillColor(colors.red)
            c.drawCentredString(X+3.75*inch, Y, " *** Special Week of Activity - CO's Visit ***")
            Y+=.5*inch

            meetings = meetingDays['CO Visit']
        else:
            Y+=.5*inch
            meetings = meetingDays['Regular']

        for day in meetings['Days']:
            info={'date':date, 'coVisit': coVisit, 'pg':c, 'startX':X, 'startY':Y, 'Title':day, 'colorFont':colors.white, 'colorBackground':colors.black}
            Y = drawScheduleTitle(self, info)
            Y += .05*inch

            for meeting in meetings[day]:
                boxYstart = Y
                info = {'date':date, 'startX':X, 'startY':Y, 'meeting':meeting, 'pg':c, 'coVisit': coVisit, 'Review':review}
                Y = drawMeeting(self, info)
                boxYend = Y
                #draw rectangle around meeting
                c.rect(X+.17*inch, boxYstart-.17*inch, 7.5*inch, boxYend - boxYstart-.03*inch)

        # add cleaning schedule
        info={'date':date, 'coVisit': coVisit, 'pg':c, 'startX':X, 'startY':Y, 'Title':'KH Cleaning Schedule', 'colorFont':colors.white, 'colorBackground':colors.forestgreen}
        Y = drawScheduleTitle(self, info)

        c.setFillColor(colors.black)
        c.setFont(myFont, myFontSize)
        c.drawString(X+.25*inch, Y, 'Light Cleaning - After every Meeting: ' + Tms.getCalendarInfo(self, date, 'LightCleaning'))
        Y += .2*inch
        c.drawString(X+.25*inch, Y, 'Heavy Cleaning - on weekend, inside and out: ' + Tms.getCalendarInfo(self, date, 'HeavyCleaning'))

        c.showPage()

    c.save()
    subprocess.call([PdfReader, fname])
    try:
        os.remove(fname)
    except:
        pass

def buildReport(self, reportInfo):
    repType = reportInfo[0]
    repDates = reportInfo[1]
    if repType == 'Worksheets':
        buildWorksheets(self, repDates)

    if repType == 'Schedule':
        buildSchedule(self, repDates)

    if repType == 'Assignment Slips':
        PdfReader = self.p.db.Options.get('Pdf Reader','')
        fname = tempfile.mktemp('.pdf')
        c = canvas.Canvas(fname, pagesize=letter)
        width, height = letter  #keep for later
        c.setFont(myFont, myFontSize)

        #first get talks for weeks selected
        for date in repDates:
            talks = ['Talk 1a','Talk 2a','Talk 3a','Talk 1b','Talk 2b','Talk 3b','Talk 1c','Talk 2c','Talk 3c']
            info = getAssignmentSlipInfo(self, date, 'Highlights')
            if info['Name'] != '':
                drawAssignmentSlip(c, info)

            if self.p.db.SourceMaterial[date]['Review'] != 'Y':
                for talk in talks:
                    info = getAssignmentSlipInfo(self, date, talk)
                    if info['Name'] != '':
                        drawAssignmentSlip(c, info)

        c.save()
        subprocess.call([PdfReader, fname])
        try:
            os.remove(fname)
        except:
            pass



if __name__ == '__main__':
    Tms.Main()
